package com.coollang.smartbra.utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.widget.Toast;

import com.coollang.smartbra.constant.Constants;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.shareboard.SnsPlatform;
import com.umeng.socialize.utils.SocializeUtils;

import java.io.File;

/**
 * @Auther: Timmy
 * @Data: 2017/9/6
 * @Fuction:
 */

public class ShareUtils {

    private Activity mActivity;
    private ProgressDialog mProgressDialog;

    public ShareUtils(Activity activity) {
        mActivity = activity;
    }

    public void initShare() {
        // 截屏
        BitmapUtils.screenShot(mActivity);
        mProgressDialog = new ProgressDialog(mActivity);
    }

    private UMShareListener mUMShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            SocializeUtils.safeShowDialog(mProgressDialog);
            LogUtils.i("ShareUtils", "开始分享");
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            SocializeUtils.safeCloseDialog(mProgressDialog);
            Toast.makeText(mActivity, "分享成功", Toast.LENGTH_SHORT).show();

            if (mResult != null)
                mResult.onShareSuccess();
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            Toast.makeText(mActivity, "分享失败", Toast.LENGTH_SHORT).show();
            SocializeUtils.safeCloseDialog(mProgressDialog);
            if (mResult!=null)
                mResult.onShareFaild();
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            SocializeUtils.safeCloseDialog(mProgressDialog);
            if (mResult!=null)
                mResult.onShareCancel();
            Toast.makeText(mActivity, "取消分享", Toast.LENGTH_SHORT).show();
        }
    };

    public void share(int i) {
        File file = new File(Constants.MY_SHARE_PATH);
        UMImage umImage = new UMImage(mActivity, file);
        SnsPlatform snsPlatform = null;

        switch (i) {
            case 1:
                // 微信
                snsPlatform = SHARE_MEDIA.WEIXIN.toSnsPlatform();
                umImage.setThumb(new UMImage(mActivity, file));
                break;
            case 2:
                snsPlatform = SHARE_MEDIA.WEIXIN_CIRCLE.toSnsPlatform();
                break;
            case 3:
                snsPlatform = SHARE_MEDIA.SINA.toSnsPlatform();
                break;
            case 4:
                snsPlatform = SHARE_MEDIA.QQ.toSnsPlatform();
                break;
        }

        new ShareAction(mActivity).withMedia(umImage)
                .withText("快来一起做运动吧.")
                .setPlatform(snsPlatform.mPlatform)
                .setCallback(mUMShareListener).share();
    }

    private ShareResult mResult;

    public void setResult(ShareResult result) {
        mResult = result;
    }

    public interface ShareResult {
        void onShareSuccess();

        void onShareFaild();

        void onShareCancel();
    }
}
